﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;

namespace VF.Ip
{

    
    public class Address
    {
        //public static Dictionary<string, int> countryidc = new Dictionary<string, int>(256);
        //public static Dictionary<string, int> provinceidc = new Dictionary<string, int>(256);
        //public static Dictionary<string, int> cityidc = new Dictionary<string, int>(400);
        //static int vcountry, vprovince, vcity = 0;
        public string country = "0";
        public string province = "0";
        public string city = "0";
        //internal static int GetAddSetCountry(string country)
        //{
        //    if (countryidc.ContainsKey(country))
        //    {
        //        return countryidc[country];
        //    }
        //    else
        //    {
        //        countryidc[country] = vcountry++;

        //        return vcountry - 1;
        //    }
        //}
        //internal static int GetAddSetProvince(string province)
        //{
        //    if (provinceidc.ContainsKey(province))
        //    {
        //        return provinceidc[province];
        //    }
        //    else
        //    {
        //        provinceidc[province] = vprovince++;

        //        return vprovince - 1;
        //    }
        //}
        //internal static int GetAddSetCity(string city)
        //{
        //    if (cityidc.ContainsKey(city))
        //    {
        //        return cityidc[city];
        //    }
        //    else
        //    {
        //        cityidc[city] = vcity++;

        //        return vcity - 1;
        //    }
        //}
        //internal static int GetPostion(string country, string province, string city)
        //{
        //    var k1 = countryidc[country];
        //    var k2 = provinceidc[province];
        //    var k3 = cityidc[city];
        //    k1 = k1 << 24;
        //    k1 = k1 | (k2 << 16);
        //    k1 = k1 | k3;
        //    return k1;
        //}
        internal static  string[] countrys;
        internal static  string[] provinces;
        internal static  string[] citys;
        private const uint MARK1 = 16711680;
        private const uint MARK2 = 65535;
        internal static Address GetAddress(int postion)
        {
            uint p = (uint)postion;
            uint country = p >> 24;
            uint province = (p & MARK1) >> 16;
            uint city = p & MARK2;
            Address address = new Address()
            {
                country = countrys[country],
                province = provinces[province],
                city = citys[city]
            };
            return address;
        }
        //internal static void AddAddress(string country, string province, string city)
        //{
        //    GetAddSetCountry(country);
        //    GetAddSetProvince(province);
        //    GetAddSetCity(city);
        //}
        //internal static void Save()
        //{
        //    using (BinaryWriter bw = new BinaryWriter(File.Create("E:\\city.bin")))
        //    {
        //        bw.Write(countryidc.Count);
        //        bw.Write(provinceidc.Count);
        //        bw.Write(cityidc.Count);

        //        string[] temp = countryidc.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
        //        foreach (var item in temp)
        //        {
        //            bw.Write(item);
        //        }

        //        temp = provinceidc.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
        //        foreach (var item in temp)
        //        {
        //            bw.Write(item);
        //        }

        //        temp = cityidc.OrderBy(x => x.Value).Select(x => x.Key).ToArray();
        //        foreach (var item in temp)
        //        {
        //            bw.Write(item);
        //        }
        //    }
        //}
        internal static void Load()
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigurationManager.AppSettings["citybin"]);
            using (var bw = new BinaryReader(File.OpenRead(path)))
            {
                var k1 = bw.ReadInt32();
                var k2 = bw.ReadInt32();
                var k3 = bw.ReadInt32();
                countrys = new string[k1];
                provinces = new string[k2];
                citys = new string[k3];
                for (int i = 0; i < k1; i++)
                {
                    countrys[i] = bw.ReadString();
                    //countryidc[countrys[i]] = i;
                }
                for (int i = 0; i < k2; i++)
                {
                    provinces[i] = bw.ReadString();
                    // provinceidc[provinces[i]] = i;
                }
                for (int i = 0; i < k3; i++)
                {
                    citys[i] = bw.ReadString();
                    // cityidc[citys[i]] = i;
                }
            }
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder(512);

            sb.Append("国家:").Append(this.country).Append("\t省份:").Append(this.province).Append("\t市区").Append(this.city);
            return sb.ToString();
        }
    }
}